﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.BLL.FoodTesting;
using StoragePeriod.BLL.RawDict;
using StoragePeriod.Model.FoodTesting;
using StoragePeriod.Model.RawDict;

namespace StoragePeriod.Web.FoodTesting.ProteinFood
{
    public partial class ProteinEdit : System.Web.UI.Page
    {
        T_FoodRecord tFoodRecord = new T_FoodRecord();  //检测记录
        T_ProteinData tProteinData = new T_ProteinData();              //检测记录
        T_RawMaterial tRawMaterial = new T_RawMaterial();//原料操作

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["RawID"] != null && Request.QueryString["FoodTime"] != null && Request.QueryString["FoodNum"] != null)
                {
                    this.LoadData();
                }
            }
        }

        //加载数据
        public void LoadData()
        {
            DateTime date = Convert.ToDateTime(Request.QueryString["FoodTime"].ToString());
            int RawId = Convert.ToInt32(Request.QueryString["RawID"].ToString());

            E_ProteinData md_ProteinDat = new E_ProteinData();
            md_ProteinDat.ProteinTime = date;
            md_ProteinDat.RawId = RawId;
            List<E_ProteinData> eProteinDat = tProteinData.GetDataListByTime(md_ProteinDat);

            E_FoodRecord md_FoodRecord = new E_FoodRecord();
            md_FoodRecord.FoodTypeID = 4;//检测类型
            md_FoodRecord.FoodTime = date;
            md_FoodRecord.RawID = RawId;
            E_FoodRecord eFoodRecord = tFoodRecord.GetFoodRecord(md_FoodRecord);

            if (eFoodRecord != null)
            {
                this.labMean.Text = eFoodRecord.FoodResult;
            }

            if (eProteinDat != null)
            {
                int FoodNum = int.Parse(Request.QueryString["FoodNum"]);
                E_ProteinData md = eProteinDat.Where(p => p.FoodNum == FoodNum).FirstOrDefault();//获取第一次检测记录
                if (md != null)
                {
                    this.txt_V1.Text = md.Protein_V1;
                    this.txt_V2.Text = md.Protein_V2;
                    //this.txt_V3.Text = md.Protein_V3;
                    this.txt_C.Text = md.Protein_C;
                    this.txt_m.Text = md.Protein_m;
                    this.txt_F.Text = md.Protein_F;
                    this.txt_PJ.Text = md.Protein_PJ;
                    this.txt_JD.Text = md.Protein_JD;
                    this.txt_JMD.Text = md.Protein_JMD;
                }
            }
        }

        //提交数据
        protected void btPost_Click(object sender, EventArgs e)
        {
            int RawId = Convert.ToInt32(Request.QueryString["RawID"]);
            DateTime date = Convert.ToDateTime(Request.QueryString["FoodTime"]);
            int FoodNum = int.Parse(Request.QueryString["FoodNum"]);
            try
            {
                double V1 = Convert.ToDouble(this.txt_V1.Text.Trim());
                double V2 = Convert.ToDouble(this.txt_V2.Text.Trim());
                //double V3 = Convert.ToDouble(this.txt_V3.Text.Trim());
                double C = Convert.ToDouble(this.txt_C.Text.Trim());
                double m = Convert.ToDouble(this.txt_m.Text.Trim());
                double F = Convert.ToDouble(this.txt_F.Text.Trim());
                double X = ((V1 - V2) * C * 0.014 / m) * F * 100;
                Convert.ToInt32(X);

                string PJ = this.txt_PJ.Text.Trim();
                string JD = this.txt_JD.Text.Trim();
                string JMD = this.txt_JMD.Text.Trim();

                E_RawMaterial eRawMaterial = tRawMaterial.GetModel(RawId);

                E_ProteinData eProteinData = new E_ProteinData();
                eProteinData.RawId = RawId;
                eProteinData.RawName = eProteinData.RawName;
                eProteinData.Protein_m = m.ToString();
                eProteinData.Protein_X = X.ToString("f2");
                eProteinData.Protein_PJ = PJ;
                eProteinData.Protein_JD = JD;
                eProteinData.Protein_JMD = JMD;
                eProteinData.Protein_V1 = V1.ToString();
                eProteinData.Protein_V2 = V2.ToString();
                eProteinData.Protein_V3 = "0";//V3.ToString();
                eProteinData.Protein_C = C.ToString();
                eProteinData.Protein_F = F.ToString();
                eProteinData.ProteinTime = date;
                eProteinData.FoodNum = FoodNum;

                tProteinData.RemoveByFoodNum(eProteinData);   //删除现有的检测记录
                tProteinData.Add(eProteinData);               //添加检测记录
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('提交成功！');</script>");
            }
            catch
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('输入有误！');</script>");
            }
        }

        //计算平均值
        protected void btnMean_Click(object sender, EventArgs e)
        {
            DateTime date = Convert.ToDateTime(Request.QueryString["FoodTime"].ToString());
            int RawId = Convert.ToInt32(Request.QueryString["RawID"].ToString());

            E_ProteinData md_ProteinData = new E_ProteinData();
            md_ProteinData.ProteinTime = date;
            md_ProteinData.RawId = RawId;
            List<E_ProteinData> eVcDataList = tProteinData.GetDataListByTime(md_ProteinData);


            E_FoodRecord md_FoodRecord = new E_FoodRecord();
            md_FoodRecord.FoodTypeID = 4;//检测类型
            md_FoodRecord.FoodTime = date;
            md_FoodRecord.RawID = RawId;
            E_FoodRecord eFoodRecord = tFoodRecord.GetFoodRecord(md_FoodRecord);

            if (eVcDataList == null || eVcDataList.Count < 1)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('请先提交检测数据！');</script>");
                return;
            }

            double Result = 0;//检测结果
            eVcDataList.ForEach(p =>
            {
                Result += Convert.ToDouble(p.Protein_X);
            });

            if (eFoodRecord != null)  //若存在则进行修改
            {
                eFoodRecord.FoodResult = (Result / eVcDataList.Count).ToString("f2");
                tFoodRecord.Update(eFoodRecord);
                this.labMean.Text = eFoodRecord.FoodResult;
            }
            else
            {
                md_FoodRecord.FoodResult = (Result / eVcDataList.Count).ToString("f2");
                E_RawMaterial eRawMaterial = tRawMaterial.GetModel(RawId);
                md_FoodRecord.RawName = eRawMaterial.RAWName;
                tFoodRecord.Add(md_FoodRecord);
                this.labMean.Text = md_FoodRecord.FoodResult;
            }
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('计算完毕！');parent.window.RefValue();</script>");
        }
    }
}